package com.fub.view.mapcomponents;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.graphics.Typeface;

import com.fub.guidance.Point;
import com.fub.util.PreferenceDAO;
import com.fub.util.Util;

public class ImageLabelComponent {

	private Paint paintToolTip;
	private Paint paintTextBold;;
	private int tooltipWidth;
	private int tooltipHeight;
	private Point zoomedPosition;
	private int color;
	private Matrix matrix;
	private Bitmap icon;
	private Canvas backgroundCanvas;
	private Bitmap backgroundBitmap;
	private PaintFlagsDrawFilter setfil;
	private PreferenceDAO preferenceDAO;
	private Point position;
	private String text;

	public ImageLabelComponent(Point position, Bitmap icon,
			PreferenceDAO preferenceDAO, int color, String text) {
		this.preferenceDAO = preferenceDAO;
		this.color = color;
		this.position = position;
		this.text = text;
		initPaints();
		this.matrix = new Matrix();
		this.icon = icon;
		this.backgroundBitmap = Bitmap.createBitmap(30, 30, Config.ARGB_4444);
		this.backgroundCanvas = new Canvas(this.backgroundBitmap);
		this.setfil = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);
	}

	public Point getPositon() {
		return this.position;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public boolean isTapped(Point eventPosition) {
		int left = this.zoomedPosition.getX()
				- this.preferenceDAO.getLeftUpperCorner().getX() - 10;
		int right = this.zoomedPosition.getX()
				- this.preferenceDAO.getLeftUpperCorner().getX()
				+ this.tooltipWidth + 10;
		int top = this.zoomedPosition.getY()
				- this.preferenceDAO.getLeftUpperCorner().getY() - 10;
		int bottom = this.zoomedPosition.getY()
				- this.preferenceDAO.getLeftUpperCorner().getY()
				+ this.tooltipHeight + 10;
		Rect bounds = new Rect(left, top, right, bottom);
		return bounds.contains(eventPosition.getX(), eventPosition.getY());
	}

	private void initPaints() {
		this.paintToolTip = new Paint(Paint.ANTI_ALIAS_FLAG);
		this.paintToolTip.setColor(this.color);
		this.paintToolTip.setStrokeWidth(1);
		this.paintToolTip.setShadowLayer(2, 5, 15, Color.argb(25, 0, 0, 0));

		// label
		this.paintTextBold = new Paint();
		this.paintTextBold.setAntiAlias(true);
		this.paintTextBold.setTypeface(Typeface.DEFAULT_BOLD);
		this.paintTextBold.setStyle(Paint.Style.FILL);
		this.paintTextBold.setTextSize(12);
		this.paintTextBold.setColor(Color.RED);
	}

	public void draw(Canvas canvas) {
		this.paintToolTip.setColor(this.color);
		this.zoomedPosition = Util.calculateZoomedPosition(this.position,
				this.preferenceDAO);
		this.tooltipWidth = this.icon.getWidth();
		this.tooltipHeight = this.icon.getHeight();

		Rect rect = new Rect(0, 0, this.tooltipWidth, this.tooltipHeight);
		this.backgroundCanvas.drawRect(rect, this.paintToolTip);
		this.backgroundCanvas.drawBitmap(this.icon, new Matrix(), null);
		this.backgroundCanvas.drawText(this.text, 10, 10, this.paintTextBold);
		this.matrix.reset();
		this.zoomedPosition.move(-this.tooltipWidth / 2,
				-this.tooltipHeight / 2);
		this.matrix.postTranslate(this.zoomedPosition.getX(),
				this.zoomedPosition.getY());
		canvas.setDrawFilter(this.setfil);
		canvas.drawBitmap(this.backgroundBitmap, this.matrix, null);
	}
}
